package S_Algorithm;

import java.util.ArrayList;
import java.util.List;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author DannyYo
 */
public class SSTF {// 最短寻道时间优先算法

    public int m; // 用来判断排序后 磁头所在的初始位置的下标
    List list = new ArrayList();
    public int b[] = new int[101];
    public int rflag = 0, lflag = 0;
    public double SUM = 0;
    public int mleft, mright;
    private int a[];

    public Object[] Calculate(int aa[], int n, int position, Object[] o) {// 两个形参分别表示磁盘请求序列
        // 磁头的初始位置
        a = aa.clone();
        int temp;
        for (int i = n; i > 0; i--) // 冒泡排序
        {
            for (int j = 0; j < i; j++) {
                if (a[j] > a[j + 1]) // 按顺序依次比较大小
                {
                    temp = a[j]; // 把大的数字往前面放
                    a[j] = a[j + 1];
                    a[j + 1] = temp;
                }
            }
        }
        if (a[n] == position) {//如果磁头的初始位置在最大号
            System.out.println("SSTF序列为：");
            for (int i = 0; i <= n; i++) {
                System.out.print(a[i] + "     ");
                list.add(a[i]);
            }


        } else if (a[0] == position) {//如果磁头的初始位置在最小号
            System.out.println("SSTF序列为：");
            for (int i = n, j = 0; i >= 0; i--, j++) {
                System.out.print(a[i] + "     ");
                list.add(a[i]);
            }
        } else {
            System.out.println("SSTF序列为：");
            Check(a, n, position);
        }

//                Object[] o = new Object[2];
        Object[] bb = list.toArray();
        for (int k = 0; k < 10; k++) {
//                    System.out.println("kkk序列为：");
//                    System.out.print(bb[k] + "     ");
            b[k + 1] = (int) bb[k];
        }
        o[2] = b;
        o[3] = SUM;
        return o;

    }

    public void Check(int a[], int n, int position) {
        boolean Run = true, Running = true;
        while (Run) {//此循环用来寻找磁头的初始位置被排到了什么位置
            for (int i = 0; i <= n; i++) {
                if (a[i] == position) {
                    m = i;
                    System.out.println("m的值为" + m);
                    mleft = m - 1;
                    mright = m + 1;
                    Run = false;
                }
            }
        }
///////////////////////////
        while (Running) {
              System.out.println("ml的值为" + mleft);
                System.out.println("mr的值为" + mright);
//                if(mright > n||mleft < 0)
//                    break;
            if (a[m] - a[mleft] >= a[mright] - a[m] && rflag != 1) {
                SUM = SUM + (a[mright] - a[m]);	//用来统计寻到长度
                m = mright;
                mright++;
                if (mright > n) {
                    rflag = 1;
                }
                System.out.print(a[m] + "   ");
                list.add(a[m]);
            } else if (a[m] - a[mleft] < a[mright] - a[m] && lflag != 1) {
                SUM = SUM + (a[m] - a[mleft]);
                m = mleft;
                mleft--;
                if (mleft < 0) {
                    lflag = 1;
                }
                System.out.print(a[m] + "   ");
                list.add(a[m]);
            }
            if (mleft != 0 && mright > n) {
                Running = false;
                SUM = SUM + a[n] - a[mleft];
                for (int j = mleft; j >= 0; j--) {
                    if (j != 0) {
                        SUM = SUM + a[j] - a[j - 1];
                    }
                    System.out.print(a[j] + "   ");
                    list.add(a[j]);
                }
            }
            if (mright != n && mleft < 0) {
                Running = false;
                SUM = SUM + a[mright] - a[0];
                for (int k = mright; k <= n; k++) {
                    if (k != n) {
                        SUM = SUM + a[k + 1] - a[k];
                    }
                    System.out.print(a[k] + "   ");
                    list.add(a[k]);
                }
            }
        }
        System.out.println();
        System.out.println("sum的值为" + SUM);
        System.out.println("平均寻到长度为" + SUM / n);
    }
}
